package com.dzsw;

import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.office.OfficeUtils;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;

import java.io.File;
import java.util.UUID;

public class JODConverterMain {
    public static void main(String[] args) {
        int[] ports = new int[]{ 18102};
        LocalOfficeManager officeManager = null;
        try {
            officeManager = LocalOfficeManager.builder()
                    .officeHome("D:/ProTools/LibreOffice")
                    .hostName("127.0.0.1")
                    .portNumbers(ports)
                    .processTimeout(120000L)
                    .maxTasksPerProcess(20)
                    .workingDir("D:/works/excelToPdf")
                    .build();
            officeManager.start();

            // create path
            String basePath = "D:\\works\\excelToPdf\\LibreOfficeStressTesting\\" + "C001";
            String input = "D:\\works\\excelToPdf\\TSD110_ja.xlsx";
            String output = basePath + "\\" + UUID.randomUUID().toString().replace("-", "") + ".pdf";

            LocalConverter documentConverter = LocalConverter.builder()
                    .officeManager(officeManager)
                    .build();

            documentConverter.convert(new File(input))
                    .to(new File(output)).as(DefaultDocumentFormatRegistry.PDF)
                    .execute();

            System.out.println("success!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != officeManager && officeManager.isRunning()) {
                OfficeUtils.stopQuietly(officeManager);
            }
        }
    }
}